package com.gitee.minimalismstyle.fresh.simple.service.impl

import com.gitee.minimalismstyle.fresh.common.core.spring.SpringContextUtils
import com.gitee.minimalismstyle.fresh.simple.api.service.SimpleDatasourceService
import org.beetl.sql.core.SQLManager
import org.beetl.sql.core.SQLManagerBuilder
import org.beetl.sql.core.UnderlinedNameConversion

object SQLManagerFactoryBean {
    private val sqlManagerMap = mutableMapOf<Long, SQLManager>()

    @Synchronized
    fun  getObject(datasourceId: Long) : SQLManager{
        return if(sqlManagerMap.containsKey(datasourceId)){
            sqlManagerMap[datasourceId]!!
        }else{
            val simpleDatasourceService : SimpleDatasourceService =  SpringContextUtils.getBean(SimpleDatasourceService::class.java)
            val ds = simpleDatasourceService.getConnectionSource(datasourceId);
            val sqlManagerBuilder = SQLManagerBuilder(ds)
            val sqlManager = sqlManagerBuilder.build()
            sqlManager.nc = UnderlinedNameConversion()
            sqlManagerMap[datasourceId] = sqlManager
            sqlManager
        }

    }
}